package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.Project;
import com.cqjtu.eecs.entity.ProjectCourse;
import com.cqjtu.eecs.entity.vo.TrainingProjectCourseVO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.IProjectCourseService;
import com.cqjtu.eecs.service.IProjectService;
import com.cqjtu.eecs.utils.Assert;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 培养方案-课程 前端控制器
 * </p>
 *
 * @author  luojun
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/project-course")
@Api(tags = "具体年级方案课程")
public class ProjectCourseController {
    @Autowired
    private IProjectCourseService projectCourseService;
    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation("年级、专业代码,查询全部课程")
    @GetMapping("/{year}/{majorCode}")
    public List<String> getCourse(
            @PathVariable("year")Integer year,
            @PathVariable("majorCode")String majorCode) {
        Assert.notNull(year, new ParamsErrorException("The year can not be null"));
        Assert.notBlank(majorCode, new ParamsErrorException("The year can not be null or blank"));
        List<Course> courses = projectCourseService.listProjectCourseByYearAndMajorCode(year, majorCode);
        if (courses.isEmpty())
            return new ArrayList<String>();
        return courses.stream().map(Course::getCourseName).collect(Collectors.toList());
    }


    @ApiOperation("年级、专业代码、课程名查询该专业下的课程大纲")
    @GetMapping("/{year}/{majorCode}/{name}")
    public String getCourseOutline(
            @PathVariable("year")Integer year,
            @PathVariable("majorCode")String majorCode,
            @PathVariable("name")String name
            ) throws JsonProcessingException {
        // 验参
        Assert.notNull(year, new ParamsErrorException("the year can not be null"));
        Assert.notBlank(majorCode, new ParamsErrorException("the majorCode can not be null or blank"));
        Assert.notBlank(name, new ParamsErrorException("the name can not be null or blank"));
        return objectMapper.writeValueAsString(projectCourseService.getCourseOutline(year,majorCode,name));
    }


    @Autowired
    private IProjectService projectService;

    @ApiOperation("通过专业代码和年级获取年级理论课程")
    @GetMapping("/theory-course-list/{majorCode}/{year}")
    public TrainingProjectCourseVO getTrainingProjectCourseVOByPid(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year){
        Assert.notNull(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notNull(year, new ParamsErrorException("年级不能为空"));
        Project project= projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return null;
        String pid=project.getPid().toString();
        return projectCourseService.getTrainingProjectCourseVOByPid(pid);
    }

    @ApiOperation("通过专业代码和年级获取模板中的实践课程")
    @GetMapping("/practical-course-list/{majorCode}/{year}")
    public TrainingProjectCourseVO getPracticalCourseListByPid(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year) {
        Assert.notNull(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notNull(year, new ParamsErrorException("年级不能为空"));
        Project project= projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return null;
        String pid=project.getPid().toString();
        return projectCourseService.getPracticalCourseListByPid(pid);
    }

}
